package api

import (
	"net/http"
	"user_api/models/response"

	"github.com/gin-gonic/gin"
	"github.com/mojocn/base64Captcha"
	"go.uber.org/zap"
)

// 存内存里了
var store = base64Captcha.DefaultMemStore

// GetCaptcha获取验证码
func GetCaptcha(c *gin.Context) {

	driver := base64Captcha.NewDriverDigit(80, 240, 5, 0.7, 80)
	cp := base64Captcha.NewCaptcha(driver, store)
	id, b64s, _, err := cp.Generate()
	if err != nil {
		zap.L().Error("生成验证码失败", zap.Error(err))
		c.JSON(http.StatusInternalServerError, response.Response{
			Code:    http.StatusInternalServerError,
			Message: "生成验证码失败",
		})
		return
	}
	zap.L().Debug("生成验证码成功", zap.String("id", id), zap.String("b64s", b64s))
	c.JSON(http.StatusOK, response.Response{
		Code:    http.StatusOK,
		Message: "success",
		Data: map[string]string{
			"captcha_id": id,
			"pic_path":   b64s,
		},
	})
}
